#!/usr/bash
# BulletYuan****author****1.0****version
echo -e "\n...BulletYuan...\n...version 1.0 ...\n\n"
argnum="$#"                 
echo -e "args number is : $argnum\n"

# output help info
_HELP_Info(){
	echo -e "looks like you need help:\n"
	echo -e "-d src savepath: the mode is that just download.just two args"
	echo -e "-dtar src savepath/tarpath: the mode is that download and zip the file.just two args; unzip file type just .tar/.tar.gz/.tar.bz2/.tar.Z/.zip"
}

# output wroung info,programmer defined wroung info text
_WROUNG_Info(){
	echo -e "something is wroung!:\n"
	echo -e "$1"
}

# output error info,programmer defined error info text
_ERROR_Info(){
	echo -e "something is error!:\n"
	echo -e "$1"
}

# download function
_DOWNLOAD_(){
	wget --user-agent="Mozilla/5.0 （Windows; U; Windows NT 6.1; en-US） AppleWebKit/534.16 （KHTML， like Gecko） Chrome/10.0.648.204 Safari/534.16" -v -c --tries=5 --timeout=30 --no-cache --output-document="$2" "$1"
	sleep 1s
}

# unzip function
_UNZIP_(){
	file="$1"
	read "the zipfile is in path:$file\nand if you wanna unzip to another directory,please keypress the abs-path\nand if you wanna unzip to this directory,just keypress ENTER skip:" unzippath
	if [ -n $unzippath ]; then
		if [ "${file##*.}"x = "gz"x ]||[ "${file##*.}"x = "bz2"x ]||[ "${file##*.}"x = "Z"x ]; then
			read "is that show the zipfile's list? [y/n]" res
			if [ -n $res]; then
				$res="$res | tr '[:upper:]' '[:lower:]'"
			else
				$res="y"
			fi
			
			if [ $res -q "y"]; then
				tar -tf "$file"
			fi
			echo -e "start unzip\n"
			tar -xf --checkpoint=2 "$file" -C "$unzippath"
		elif [ "${file##*.}"x = "zip"x ]; then
			read "is that show the zipfile's list? [y/n]" res
			if [ -n $res]; then
				$res="$res | tr '[:upper:]' '[:lower:]'"
			else
				$res="y"
			fi
			
			if [ $res -q "y"]; then
				unzip -l "$file"
			fi
			echo -e "start unzip\n"
			tar -n "$file" -d "$unzippath"
		fi
	else
		echo -e "the unzip path had wroung input!please retry!\n"
		_UNZIP_ "$file"
	fi
}

# download&unzip function
_DOWNLOAD_UNZIP_(){
	wget --user-agent="Mozilla/5.0 （Windows; U; Windows NT 6.1; en-US） AppleWebKit/534.16 （KHTML， like Gecko） Chrome/10.0.648.204 Safari/534.16" -v -c --tries=5 --timeout=30 --no-cache --output-document="$2" "$1"
	sleep 1s
	_UNZIP_ "$2"
}

# main program enter

if [ "$argnum" -eq "0" ]; then
	_HELP_Info
else
	arg1="$1"
	case $arg1 in

		-d)
			if [ -n "$2" ]; then
				if [ -n "$3" ]; then
					echo -e "just download;\nsrc: $2\ntarget: $3\n"
					_DOWNLOAD_ "$2" "$3"
				else
					wtxt="arg:target can not be noneType or empty!"
					_ERROR_Info "$wtxt"
				fi
			else
				wtxt="arg:src can not be noneType or empty!"
				_ERROR_Info "$wtxt"
			fi
			;;
		-dtar)
			if [ -n "$2" ]; then
				if [ -n "$3" ]; then
					echo -e "download and unzip;\nsrc $2\ntarget: $3\n"
					_DOWNLOAD_UNZIP_ "$2" "$3"
				else
					wtxt="arg:target can not be noneType or empty!"
					_ERROR_Info "$wtxt"
				fi
			else
				wtxt="arg:src can not be noneType or empty!"
				_ERROR_Info "$wtxt"
			fi
			;;
		-h)
			_HELP_Info
			;;
		-help)
			_HELP_Info
			;;
		*)
			_HELP_Info
			;;

	esac

fi